import { Commit } from 'vuex';
import { IState } from './state';
import { IRoute } from '@/typings';
import { SET_ROUTE_TREE, SET_AUTH } from './actionTypes';
import { getUserRouteList } from '@/services';
import { formatRouteTree } from '@/libs';

export default {
  async [SET_ROUTE_TREE]({ commit, state }: { commit: Commit; state: IState }) {
    const routeList = (await getUserRouteList(
      state.uid
    )) as unknown as IRoute[];

    //将列表转为树形结构
    const routeTree = formatRouteTree(routeList);

    //修改state数据
    commit(SET_ROUTE_TREE, routeTree);
    commit(SET_AUTH, true);
  },
  [SET_AUTH]() {}
};
